﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace hse_2013_sudoku
{
    class Field
    {
        private int[,] desk = new int[9, 9];

        public int[,] CutSquare(int i, int j)
        {
            int length = Convert.ToInt32(Math.Sqrt(desk.GetLength(0))); //длина стороны мини-квадрата
            int[,] sq = new int[length, length]; //сам мини-квадрат
            if (length == Math.Sqrt(desk.GetLength(0))) //проверка возможности деления на квадраты
            {
                int dim1 = i / length;
                int dim2 = j / length;
                for (int a = dim1 * length; a < ((dim1 + 1) * length); a++)
                {
                    for (int b = dim2 * length; b < ((dim2 + 1) * length); b++)
                    {
                        sq[a - dim1 * length, b - dim2 * length] = desk[a, b];
                    }
                }
                return sq;
            }
            else { return null; } //если нельзя поделить на квадраты
        }

        public int[,] Getmtr()
        {
            return desk;
        }

        public void SetMtr(int[,] anything)
        {
            desk = anything;
        }

        public Field()
        {

        }

        private void Seed()
        {
            List<int> fewnums;

            for (int i = 0; i < 1; i++)
            {

                for (int j = 0; j < desk.GetLength(1); j++)
                {
                    fewnums = Test.allowedNumbers(this, i, j);
                    int[] fewnumsarr = fewnums.ToArray();
                    int length = fewnumsarr.Length - 1;
                    Random g = new Random();
                    int h = g.Next(length);
                    desk[i, j] = fewnums.ToArray()[h];
                }
            }
        }

        public void Fill()
        {
            Seed();
            int[] a = new int[desk.GetLength(1)];

            for (int i = 1; i < desk.GetLength(0); i++)
            {
                if (i % 3 != 0)
                {
                    for (int j = 0; j < desk.GetLength(1); j++)
                    {
                        if (j + 3 < desk.GetLength(1))
                        {
                            desk[i, j] = desk[i - 1, j + 3];
                        }
                        else
                        {
                            desk[i, j] = desk[i - 1, j + 3 - desk.GetLength(1)];
                        }
                    }
                }
                else
                {
                    for (int j = 0; j < desk.GetLength(1); j++)
                    {
                        if (j + 4 < desk.GetLength(1))
                        {
                            desk[i, j] = desk[i - 1, j + 4];
                        }
                        else
                        {
                            desk[i, j] = desk[i - 1, j + 4 - desk.GetLength(1)];
                        }
                    }
                }
            }
        }
    }
}
